package com.cljz.makepassenger.ui.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.model.net.bean.ActivityListBean;
import com.cljz.makepassenger.ui.holder.NearByListHolder;

import java.util.List;

/**
 * Created by apple on 2017/4/26.
 */

public class NearByListAdapter extends RecyclerView.Adapter<NearByListHolder> {
    private Context context;
    private List<ActivityListBean.DataBean> activityList;
    private OnItemClickListener mOnItemClickListener;


    public void setData(List<ActivityListBean.DataBean> activityList) {
        this.activityList = activityList;
        notifyDataSetChanged();
    }

    public void setDataWithoutNotify(List<ActivityListBean.DataBean> activityList) {
        this.activityList = activityList;
    }
    public void setDataWithChangePosition(List<ActivityListBean.DataBean> activityList, int changePosition) {
        this.activityList = activityList;
        notifyItemChanged(changePosition);
    }

    public void addData(List<ActivityListBean.DataBean> activityList) {
        int startPosition = this.activityList.size() + 1;
        this.activityList.addAll(activityList);
        notifyItemRangeInserted(startPosition,activityList.size());
    }


    public NearByListAdapter(Context context, List<ActivityListBean.DataBean> activityList) {
        this.context = context;
        this.activityList = activityList;
    }


    @Override
    public NearByListHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new NearByListHolder(View.inflate(context, R.layout.item_nearby, null));
    }

    @Override
    public void onBindViewHolder(final NearByListHolder holder, final int position) {
        holder.setData(activityList, position);
        if (mOnItemClickListener != null) {
            holder.itemView.findViewById(R.id.iv_activity_collect).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onCollectClick(activityList,position);
                }
            });

            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onClick(activityList,position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return activityList == null ? 0 : activityList.size();
    }

    public interface OnItemClickListener {
        void onClick(List<ActivityListBean.DataBean> activityList,int position);
        void onCollectClick(List<ActivityListBean.DataBean> activityList,int position);

        void onLongClick(ActivityListBean.DataBean dataBean);
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }
}
